#include "doc_editor_menu_bar_widget.h"
#include "ui_doc_editor_menu_bar_widget.h"
#include "create_table_dialog.h"
#include "easylogging++.h"

DocEditorMenuBarWidget::DocEditorMenuBarWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DocEditorMenuBarWidget),
    m_sizeNameToPixelSize(new QHash<QString, int>),
    m_pixelSizeToSizeName(new QHash<int, QString>)
{
    ui->setupUi(this);
    initUi();
    initEvents();
}

DocEditorMenuBarWidget::~DocEditorMenuBarWidget()
{
    delete ui;
}

void DocEditorMenuBarWidget::setFontFamily(QString fontFamily)
{
    int index = ui->fontSelectComboBox->findText(fontFamily);
    if (-1 == index)
    {
        ui->fontSelectComboBox->addItem(fontFamily);
    }

    ui->fontSelectComboBox->setCurrentText(fontFamily);
}

QString DocEditorMenuBarWidget::getFontSizeNameByPixelSize(int pixelSize)
{
    if (m_pixelSizeToSizeName->contains(pixelSize))
    {
        return m_pixelSizeToSizeName->value(pixelSize);
    }
    else
    {
        LOG(ERROR) << "pixel size to size name not find the map of " << pixelSize;
        return QString();
    }
}

void DocEditorMenuBarWidget::setFontSize(int fontSize)
{
    if (m_pixelSizeToSizeName->contains(fontSize))
    {
        ui->fontSizeSelectComboBox->setCurrentText(m_pixelSizeToSizeName->value(fontSize));
    }
    else
    {
        LOG(ERROR) << "pixel size to size name not find the map of " << fontSize;
    }
}

void DocEditorMenuBarWidget::setFontBold(bool enabled)
{
    ui->fontBoldBtn->setChecked(enabled);
}

void DocEditorMenuBarWidget::setFontIncline(bool enabled)
{
    ui->fontInclineBtn->setChecked(enabled);
}

void DocEditorMenuBarWidget::initUi()
{
    ui->separator->setObjectName("separator");
    ui->stackedWidget->setCurrentIndex(0);
    ui->fontSelectComboBox->setEditable(true);

    setAttribute(Qt::WA_StyledBackground);
    initFontFamilyComboBox();
    initFontSizeComboBox();
}

void DocEditorMenuBarWidget::initEvents()
{
    connect(ui->startBtn, &QPushButton::clicked, this, [=]() {
        ui->stackedWidget->setCurrentIndex(0);
    });

    connect(ui->insertBtn, &QPushButton::clicked, this, [=]() {
        ui->stackedWidget->setCurrentIndex(1);
    });

    connect(ui->insertTableBtn, &QPushButton::clicked, this, [=]() {
        CreateTableDialog ctd;
        ctd.exec();
        emit(insertTable(ctd.rows(), ctd.cols()));
    });

    connect(ui->saveBtn, &QPushButton::clicked, this, [=](){
        emit(saveTriggered());
    });

    connect(ui->fontSelectComboBox, &QComboBox::currentTextChanged, this, [=](const QString& text) {
        emit(selectFontFamily(text));
    });
    connect(ui->fontSizeSelectComboBox, &QComboBox::currentTextChanged, this, [=](const QString& text) {
        if (m_sizeNameToPixelSize->contains(text))
        {
            emit(selectFontSize(m_sizeNameToPixelSize->value(text)));
        }
        else
        {
            LOG(ERROR) << "size name to pixel size not find the map of " << text.toStdString();
        }
    });
    connect(ui->fontBoldBtn, &QPushButton::clicked, this, &DocEditorMenuBarWidget::fontBold);
    connect(ui->fontInclineBtn, &QPushButton::clicked, this, &DocEditorMenuBarWidget::fontIncline);
}

void DocEditorMenuBarWidget::initFontFamilyComboBox()
{
    ui->fontSelectComboBox->clear();
    ui->fontSelectComboBox->addItems({
                                         QStringLiteral("仿宋"),
                                         QStringLiteral("华文中宋"),
                                         QStringLiteral("华文仿宋"),
                                         QStringLiteral("华文宋体"),
                                         QStringLiteral("华文彩云"),
                                         QStringLiteral("华文新魏"),
                                         QStringLiteral("华文楷体"),
                                         QStringLiteral("华文琥珀"),
                                         QStringLiteral("华文细黑"),
                                         QStringLiteral("华文行楷"),
                                         QStringLiteral("华文隶书"),
                                         QStringLiteral("宋体"),
                                         QStringLiteral("幼圆"),
                                         QStringLiteral("微软雅黑"),
                                         QStringLiteral("微软雅黑 Light"),
                                         QStringLiteral("新宋体"),
                                         QStringLiteral("方正姚体"),
                                         QStringLiteral("方正等线"),
                                         QStringLiteral("方正舒体"),
                                         QStringLiteral("楷体"),
                                         QStringLiteral("等线"),
                                         QStringLiteral("等线 Light"),
                                         QStringLiteral("隶书"),
                                         QStringLiteral("黑体"),
                                         QStringLiteral("Consolas"),
                                         QStringLiteral("Courier New"),
                                         QStringLiteral("Times New Roman")
                                     });
}

void DocEditorMenuBarWidget::initFontSizeComboBox()
{
    if (nullptr == m_sizeNameToPixelSize || nullptr == m_pixelSizeToSizeName)
    {
        LOG(ERROR) << "size name to size num hash or size num to size name hash is nullptr!";
        return ;
    }
    QList<QPair<QString, int> > sizeNameToNum = {
        {QStringLiteral("5"), 6},
        {QStringLiteral("5.5"), 7},
        {QStringLiteral("6.5"), 8},
        {QStringLiteral("7.5"), 10},
        {QStringLiteral("9"), 12},
        {QStringLiteral("10.5"), 14},
        {QStringLiteral("12"), 16},
        {QStringLiteral("14"), 18},
        {QStringLiteral("15"), 20},
        {QStringLiteral("16"), 21},
        {QStringLiteral("18"), 24},
        {QStringLiteral("22"), 29},
        {QStringLiteral("24"), 32},
        {QStringLiteral("26"), 34},
        {QStringLiteral("36"), 48},
        {QStringLiteral("42"), 56},
        {QStringLiteral("54"), 71},
        {QStringLiteral("63"), 83},
        {QStringLiteral("72"), 95},
        {QStringLiteral("初号"), 56},
        {QStringLiteral("小初"), 48},
        {QStringLiteral("一号"), 34},
        {QStringLiteral("小一"), 32},
        {QStringLiteral("二号"), 29},
        {QStringLiteral("小二"), 24},
        {QStringLiteral("三号"), 21},
        {QStringLiteral("小三"), 20},
        {QStringLiteral("四号"), 18},
        {QStringLiteral("小四"), 16},
        {QStringLiteral("五号"), 14},
        {QStringLiteral("小五"), 12}
    };

    ui->fontSizeSelectComboBox->clear();
    for (QList<QPair<QString, int>>::iterator it = sizeNameToNum.begin(); it != sizeNameToNum.end(); it++)
    {
        m_sizeNameToPixelSize->insert(it->first, it->second);
        m_pixelSizeToSizeName->insert(it->second, it->first);
        ui->fontSizeSelectComboBox->addItem(it->first);
    }
}
